﻿/*
  Project:       DataAnalyse
  File:          SearchItemsHandler.ashx
  Description:   
 *               The global verialbles are all in this file.
  Author:        Lanny(damon_lan@163.com)
  Date:          Sept,2014
  version:       2.0
  Modify:
  
  
  
  References:

 
                 
 Comments:
 * Add log function.


Notes:
 * 
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml;
using DataAnalyseTool.Utilities;

namespace DataAnalyseTool.BusinessTier.Handlers
{
  /// <summary>
  /// Summary description for SearchItemsHandler
  /// </summary>
  public class SearchItemsHandler : IHttpHandler
  {
    protected static readonly log4net.ILog log =
       log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    public void ProcessRequest(HttpContext context)
    {
      context.Response.ContentType = "text/plain";
      string action = context.Request.Params["action"];
      switch (action)
      {
        case "Get":
          GetSearchItems(context);
          break;
        case "Set":
          SetSearchItem(context);
          break;
        default:
          break;

      }


    }

    #region Get Search Item
    private void GetSearchItems(HttpContext ctx)
    {
      string defineSearchItemRelatePath, defineSearchItemPath, result;

      defineSearchItemRelatePath = GlobalVar.configRelatePath + GlobalVar.defineSearchItemName + GlobalVar.xmlSubffix;
      defineSearchItemPath = ctx.Server.MapPath(defineSearchItemRelatePath);
      result = String.Empty;

      XmlNodeList lists = XmlOpts.GetNodeListFromXML(defineSearchItemPath, GlobalVar.xmlRequiredItemRootName);
      for (int i = 0; i < lists.Count; i++)
      {
        if (lists[i].Attributes["isSearch"].Value == "true")
          result += lists[i].Name + ";";
      }
      if (result != "")
        result = result.Substring(0, result.Length - 1);

      //log.Info("获取搜索字段信息为：" + result.ToString());
      ctx.Response.Write(result);
    }
    #endregion

    #region Set Search Item(搜索字段范围的设定)
    private void SetSearchItem(HttpContext ctx)
    {
      string str = ctx.Request.QueryString["val"].ToString();

      string fileRelatePath = GlobalVar.configRelatePath + GlobalVar.defineSearchItemName + GlobalVar.xmlSubffix;
      string filePath = ctx.Server.MapPath(fileRelatePath);
      try
      {
        XmlDocument xml = new XmlDocument();
        xml.Load(filePath);
        XmlNode root = xml.SelectSingleNode(GlobalVar.xmlRequiredItemRootName);
        XmlNodeList list = root.ChildNodes;
        if (str.Trim() == "")
        {
          SetAllProjectsFalse(list, xml, filePath);
          ctx.Response.Write("没有设置任何搜索字段.");
        }
        else
        {

          str = str.Substring(0, str.Length - 1);
          string[] allItems = str.Split(';');
          SetAllProjectsFalse(list, xml, filePath);
          SetSomeProjectTrue(list, xml, filePath, allItems);
          ctx.Response.Write("设置搜索项目成功");
        }
      }
      catch (Exception ex)
      {
        ctx.Response.Write("设置搜索项目失败");
        log.Error("Set search item failed, information: " + ex.ToString());
      }
    }
    private void SetAllProjectsFalse(XmlNodeList nodeList, XmlDocument xml, string path)
    {
      for (int i = 0; i < nodeList.Count; i++)
      {
        XmlElement ement = (XmlElement)nodeList[i];
        ement.SetAttribute("isSearch", "false");
      }
      xml.Save(path);
    }
    private void SetSomeProjectTrue(XmlNodeList nodeList, XmlDocument xml, string path, string[] allItems)
    {
      for (int i = 0; i < nodeList.Count; i++)
      {
        for (int j = 0; j < allItems.Length; j++)
        {
          if (nodeList[i].Name == allItems[j])
          {
            XmlElement xe = (XmlElement)nodeList[i];
            xe.SetAttribute("isSearch", "true");
          }
        }
      }
      xml.Save(path);
    }

    #endregion

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}